<template>
  <div class="article">
    <van-cell v-for="(item, i) in list" :key="i">
      <div class="cell-header">
        <van-image
          width="1rem"
          height="1rem"
          fit="contain"
          :src="
            item.original_status.user.photo_domain +
            item.original_status.user.profile_image_url.split(',')[0]
          "
        />
        <div class="header-right">
          <div class="name">{{ item.original_status.user.screen_name }}</div>
          <div class="time">{{ item.original_status.timeBefore }}</div>
        </div>
      </div>
      <div class="cell-content" v-html="item.original_status.description"></div>
      <div class="cell-footer">
        <div class="item">
          <van-icon name="share-o" />
          <div class="number">{{ item.original_status.retweet_count }}</div>
        </div>
        <div class="item">
          <van-icon name="smile-comment-o" />
          <div class="number">{{ item.original_status.reply_count }}</div>
        </div>
        <div class="item">
          <van-icon name="good-job-o" />
          <div class="number">{{ item.original_status.like_count }}</div>
        </div>
      </div>
    </van-cell>

    <div v-if="finish" class="finish">已加载完毕</div>
  </div>
</template>

<script>
import * as api from "@/api/index.js";
export default {
  data() {
    return {
      page: 1,
      list: [],
      finish: false,
    };
  },
  methods: {
    async getList() {
      let res = await api.getArticles({ page: this.page });
      if (res.result && res.result.indexOf("加载完") !== -1) {
        this.finish = true;
      } else {
        this.list = this.list.concat(res.items);
        this.page++;
      }
    },
    debance(fn, delay) {
      let timer = null;
      return function () {
        if (timer) {
          clearTimeout(timer);
          timer = setTimeout(fn, delay);
        } else {
          timer = setTimeout(fn, delay);
        }
      };
    },
  },
  async mounted() {
    this.getList();
    window.addEventListener(
      "scroll",
      this.debance(() => {
        let scrollHeight = document.documentElement.scrollHeight;
        let scrollTop = document.documentElement.scrollTop;
        let inneerHeight = window.innerHeight;
        console.log(scrollTop);
        if (scrollHeight - scrollTop - inneerHeight < 50) {
          this.getList();
        }
      }, 250)
    );
  },
};
</script>

<style lang="less" scoped>
.article {
  .cell-header {
    margin-bottom: 0.3rem;
    display: flex;
    gap: 0.5rem;
    .header-right {
      font-size: 0.3rem;
      line-height: 0.5rem;
      .time {
        color: #999;
      }
    }
  }
  .cell-footer {
    margin-top: 0.2rem;
    display: flex;
    justify-content: space-around;
    align-items: center;
    .item {
      display: flex;
      justify-content: center;
      align-items: center;
      gap: 0.2rem;
    }
  }
  .finish {
    color: #bbb;
    height: 0.8rem;
    line-height: 0.8rem;
    font-size: 0.4rem;
    text-align: center;
    background-color: #efefef;
  }
}
</style>
